$(document).ready(function(){
	
	//回到顶部按钮对象
	var topcontrol = $('#topcontrol')
	//导航条对象
	var nav= $('.nav-wrap');
	//导航条的实际高度
	var navHeight = nav.outerHeight();
	//导航条相当于网页原点的位置
	var navPos = nav.offset().top;
	//滚动条事件
	$(window).scroll(function(){
		//获得当前滚动条位置
		var sTop = $(window).scrollTop();
		//动态显示隐藏回到顶部的按钮
		if(sTop>=200){
			topcontrol.fadeIn(500);
		}else{
			topcontrol.fadeOut();
		}
		
		//自动给导航条设置fixed样式
		if(sTop>=navPos){
			if(!nav.hasClass('fix')){
				nav.addClass('fix');
				$('.banner').css('margin-bottom',navHeight);//fix会脱离文档输出流，用marg站位
			}
		}else{
			if(nav.hasClass('fix')){
				nav.removeClass('fix');
				$('.banner').css('margin-bottom',0);
			}
		}
		//高亮函数
		function active(obj){
			//删除其他的高亮
			$('.nav-wrap li').removeClass('active');
			//给制定的加高亮
			if(!obj.hasClass('active')){
				obj.addClass('active');
			}
		}
		
		//海量正版免费下滚动监听
		var introPos = {
			start:$('#post-intro').offset().top - navHeight,
			end:$('#post-intro').offset().top + $('#post-intro').outerHeight() - navHeight
		}
		
		if(sTop>=introPos.start && sTop<introPos.end ){
			active( $('.intro') );
		}else{
			$('.intro').removeClass('active');
		}
		//无需苹果账号
		var usagePos = {
			start:$('#post-usage').offset().top - navHeight,
			end:$('#post-usage').offset().top + $('#post-usage').outerHeight() - navHeight
		}
		
		if(sTop>=usagePos.start && sTop<usagePos.end ){
			active( $('.usage') );
		}else{
			$('.usage').removeClass('active');
		}
		//手机瘦身
		var choicePos = {
			start:$('#post-choiceness').offset().top - navHeight,
			end:$('#post-choiceness').offset().top + $('#post-choiceness').outerHeight() - navHeight
		}
		
		if(sTop>=choicePos.start && sTop<choicePos.end ){
			active( $('.choice') );
		}else{
			$('.choice').removeClass('active');
		}
		
	});
	//回到顶部
	topcontrol.click(function(){
		$('html,body').animate({scrollTop:0},1000);
	});
	
	//锚点链接过渡动画
	$('.nav-wrap a').click(function(){
		//获得对应板块相当于网页原点的位置
		var objTop = $(this.hash).offset().top - navHeight+8;
		
		$('html,body').animate({scrollTop:objTop},1000);
		
		return false;
	});
	
	
});
